package com.hrms.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @AUTHOR LK
 * @CREATE 2021-10-15-13:53
 */
@Component
public class EmailUtil {
	//注入邮件发送服务类
	@Autowired
	JavaMailSender javaMailSender;

	@Value("${spring.mail.username}")
	private String fromEmail;

	/**
	 *
	 * @param toEmail 收件人
	 * @param subject 标题
	 * @param content  邮件正文
	 * @return
	 */
	public String sendEmail(String toEmail,String title,String content){

		try{
			//创建一个普通邮件对象
			SimpleMailMessage message = new SimpleMailMessage();
			//发送者邮箱
			message.setFrom(fromEmail);
			//接收者邮箱
			message.setTo(toEmail);
			//邮件标题
			message.setSubject(title);
			//邮件正文
			message.setText(content);
			//发送邮箱
			javaMailSender.send(message);
			System.out.println("邮箱发送成功");
			return "success";
		}catch (Exception e){
			e.printStackTrace();
			System.out.println("邮箱发送失败");
		}

		return "fail";
	}
}
